package com.zdsoft.blp.zongbuju.viewpage.fragMent;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.tencent.connect.share.QQShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.gongyong.dialog.ShowOrDismissDialog;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.net.WebServiceUtil;
import com.zdsoft.blp.zongbuju.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by pc on 2016/8/30.
 */
public class FenXiangFragment extends CustomFragment implements NetCallback,View.OnClickListener{
    private static final  int CODE=1;
    private ImageView iv_wxdown,iv_androiddown;
    private static final String APP_ID_QQ="1105617853";
    public static final String APP_ID="wx7cee84958fa7d152";
    private IWXAPI api;     //  第三方和微信通信的openapi接口
    private Tencent mTencent;
    private String desc;    //共享页面内容
    private String android_download;    //安卓客户端下载地址
    private String title;   //共享内容标题
    private String logo;    //共享内容图标
    LinearLayout ll_message,ll_shareToQQ,ll_wxfrend,ll_frends;
    @Override
    protected void init() {
        initLayout(R.layout.fenxiang);

        //实例化两种下载二维码控件
        iv_wxdown = find(R.id.iv_wxdown);
        iv_androiddown= find(R.id.iv_androiddown);
        getNet();                   //加载网络数据
        mTencent = Tencent.createInstance(APP_ID_QQ,getActivity());    //创建QQ分享实例
        //通过WXAPI工厂获取IWXAPI实例
        api = WXAPIFactory.createWXAPI(getActivity(),APP_ID);
        //将应用appid注册到微信、
        api.registerApp(APP_ID);
        //实例化四种分享方式，并添加点击事件
        ll_message=find(R.id.ll_message);
        ll_shareToQQ=find(R.id.ll_shareToQQ);
        ll_wxfrend=find(R.id.ll_wxfrend);
        ll_frends=find(R.id.ll_frends);

        ll_message.setOnClickListener(this);
        ll_shareToQQ.setOnClickListener(this);
        ll_wxfrend.setOnClickListener(this);
        ll_frends.setOnClickListener(this);

    }
    //加载网络连接
    public void getNet(){
        Net net = new Net();
        net.url = API.CLIENT_URL;
        net.nameSpace=API.CLIENT_NAMESPACE;
        net.methodName=API.GETSHARECONTENT;
        net.code=CODE;
        //parems所需参数为空
        ((MainActivity)getActivity()).getServer(net,this);
    }
    //解析json数据
    @Override
    public void onResponse(boolean flag, int code, String res) {
        try {
            JSONObject object = new JSONObject(res);
            String weixin = object.getString("weixin");
            String android = object.getString("android");
            desc = object.getString("desc");
            android_download = object.getString("android_download");
            title = object.getString("title");
            logo=object.getString("logo");
            MyApp.setImage(weixin,iv_wxdown);
            MyApp.setImage(android,iv_androiddown);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ShowOrDismissDialog.dismiss();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode,resultCode,data,(IUiListener)getActivity());
        //mTencent.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //短信分享
            case R.id.ll_message:
                Uri uri = Uri.parse("smsto:");
                Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
                intent.putExtra("sms_body","社区帮帮手机客户端下载：http://114.215.193.48:8080/mo/client/download.mo");
                startActivity(intent);
                break;
            case R.id.ll_shareToQQ:
                while (WebServiceUtil.isNetworkAvailable(getActivity())==true){
                    shareToQQ();
                    break;
                };
                break;
            case R.id.ll_wxfrend:
                while (WebServiceUtil.isNetworkAvailable(getActivity())==true){
                    shareToWX(0);
                    break;
                }
                break;
            case R.id.ll_frends:
                while (WebServiceUtil.isNetworkAvailable(getActivity())==true){
                    shareToWX(1);
                    break;
                }
                break;
        }
    }
    /** *
     *自定义方法，分享到微信
     * @param flag(0:分享到微信好友，1：分享到微信朋友圈)
     */
    private  void shareToWX(int flag){
        String URL ="http://114.215.193.46:8080/static/app/images/logo.png";
        String Title="社区帮帮 让生活更简单";
        String Detail=":来自：BianLiJia";
        // 初始化一个WXTextObject对象
        WXWebpageObject webpage  = new WXWebpageObject();
        webpage.webpageUrl = URL;

        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title=Title;
        msg.description =Detail;
        //设置图片大小
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.logo);
        Bitmap thumbImage = Bitmap.createScaledBitmap(bitmap,150,150,true);
        bitmap.recycle();
        msg.setThumbImage(thumbImage);
        // 构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());;//唯一标示符，当前毫秒数
        req.message=msg;
        //表示发送给朋友圈
        // req.scene= SendMessageToWX.Req.WXSceneTimeline;
        //req.scene = flag==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;

        if(flag==0){
            req.scene=SendMessageToWX.Req.WXSceneSession;
        }else{
            req.scene=SendMessageToWX.Req.WXSceneTimeline;
        }
        //调用接口发送数据到微信
        api.sendReq(req);

    }
    //自定义方法，分享到QQ
    private  void shareToQQ(){
        Bundle bundle = new Bundle();
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE,title);  //标题
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL,android_download);//下载链接
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,logo); //图标
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY,desc);  //内容
        mTencent.shareToQQ(getActivity(),bundle,(IUiListener)getActivity());
    }
}
